import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '@/components/login.vue'
Vue.use(VueRouter);
const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home', component: () => import('@/views/home.vue'), redirect: '/welcome',
      children: [
        { path: '/welcome', component: () => import('@/components/welcome.vue') },
        { path: '/users', component: () => import('@/components/users/user.vue') },
        { path: '/roles', component: () => import('@/components/power/roles.vue') },
        { path: '/rights', component: () => import('@/components/power/Rights.vue') }
      ]
    }
  ]
})
router.beforeEach(function (to, from, next) {
  if (to.path == "/home") {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  } else {
    next();
  }
})
export default router